﻿using Smokey.Client.Linq;
using Smokey.Shared.Constants;
using Smokey.Shared.DataTransferObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Application = Smokey.Client.Linq.Application;
namespace Smokey.Client.Queries {
    public static class ApplicationQueries {

        private static Expression<Func<Application, ApplicationDto>> applicationRowToDto = a =>
            new ApplicationDto() {
                ApplicationId = a.ApplicationId,
                ApplicationName = a.ApplicationName,
                Email = a.Email,
                LastRunDate = a.LastRunDate,
                Location = a.Location,
                Quality = a.Quality,
                Status = ApplicationStatusConstant.GetConstantByValue(a.Status),
                StartDate = a.StartDate,
                EndDate = a.EndDate,
                NoOfIterations = a.NoOfIterations,
            };

        public static IList<ApplicationDto> GetAllApplications() {
            using (DBSession session = DBSession.CreateDbSession()) {
                using (ModelDataContext context = ModelDataContext.CreateDataContext(session)) {
                    return context.Applications
                        .Select(applicationRowToDto)
                        .ToList();
                }
            }
        }

        public static ApplicationDto UpdateApplication(ApplicationDto application) {
            using (DBSession session = DBSession.CreateDbSession()) {
                using (ModelDataContext context = ModelDataContext.CreateDataContext(session)) {
                    Application row;

                    if (application.ApplicationId.HasValue) {
                        row = context.Applications
                            .Where(m => m.ApplicationId == application.ApplicationId.Value)
                            .SingleOrDefault();
                    } else {
                        row = new Application();
                        context.Applications.InsertOnSubmit(row);
                    }

                    row.ApplicationName = application.ApplicationName;
                    row.Email = application.Email;
                    row.LastRunDate = application.LastRunDate;
                    row.Location = application.Location;
                    row.Quality = application.Quality;
                    row.Status = application.Status.Value;

                    context.SubmitChangesAndCompleteTransaction();

                    application.ApplicationId = row.ApplicationId;
                    return application;
                }
            }
        }

        public static ApplicationDto GetApplication(int applicationId) {
            using (DBSession session = DBSession.CreateDbSession()) {
                using (ModelDataContext context = ModelDataContext.CreateDataContext(session)) {
                    return context.Applications
                        .Where(a => a.ApplicationId == applicationId)
                        .Select(applicationRowToDto)
                        .SingleOrDefault();
                }
            }
        }
    }
}
